/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 *
 * @author Kvest
 */
public class Utils 
{
    public static String timeToString(Date date)
    {
        Calendar c = Calendar.getInstance(TimeZone.getDefault());
        c.setTime(date);
        int h = c.get(Calendar.HOUR_OF_DAY);
        int m = c.get(Calendar.MINUTE);
        String t = (h < 10 ? "0" : "") + h + ":" + (m < 10 ? "0" : "") + m;
        return t;
    }
    
    public static byte[] intToByte(int val)
    {
        byte t;
        int temp;
        byte[] result = new byte[4];  
        Integer i = new Integer(val);
        t = i.byteValue();
        result[0] = t;
        temp = i.intValue();
        temp = temp>>8;
        i = new Integer(temp);
        t = i.byteValue();
        result[1] = t;
        temp = i.intValue();
        temp = temp>>8;
        i = new Integer(temp);
        t = i.byteValue();
        result[2] = t;
        temp = i.intValue();
        temp = temp>>8;
        i = new Integer(temp);
        t = i.byteValue();
        result[3] = t;
        return result;
    }
    
    public static byte[] int16ToByte(int val)//������� �������� �� int32 � ������ byte
    {
        byte t;
        int temp;
        byte[] result;
        result = new byte[2];
        Integer i = new Integer(val);
        t = i.byteValue();
        result[0] = t;
        temp = i.intValue();
        temp = temp>>8;
        i = new Integer(temp);
        t = i.byteValue();
        result[1] = t;
        return result;
    }
    
    public static int byteToInt(byte[] val)
    {
        int result = 0;
        long bitValue = 1;
        for (int i = 0; i < val.length; ++i)
        {
            for (int j = 1; j <= 128; j *= 2)
            {
                if ((val[i] & j) == j)
                {
                    result |= bitValue;
                }

                bitValue *= 2;
            }
        }

        return result;
    }
    
    public static long byteToLong(byte[] val)
    {
        long result = 0;
        long bitValue = 1;
        for (int i = 0; i < val.length; ++i)
        {
            for (int j = 1; j <= 128; j *= 2)
            {
                if ((val[i] & j) == j)
                {
                    result |= bitValue;
                }

                bitValue = bitValue << 1;
            }
        }

        return result;
    }
    
    public static boolean byteToBoolean(byte data)
    {
        return (data != 0);
    }
}
